package org.jeecg.modules.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.flow.dto.TaskPermissionConfigDto;
import org.jeecg.modules.flow.entity.FlowPermission;
import org.jeecg.modules.flow.service.IFlowPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: 流程环节权限控制类
 * @Author: jeecg-boot
 * @Date: 2021-11-14
 * @Version: V1.0
 */
@Api(tags = "流程环节权限管理")
@RestController
@RequestMapping("/flow/permission")
@Slf4j
public class FlowPermissionController extends JeecgController<FlowPermission, IFlowPermissionService> {
	@Autowired
	private IFlowPermissionService flowPermissionService;

	/**
	 * 分页列表查询
	 *
	 * @param flowPermission 实体类
	 * @param pageNo         页码
	 * @param pageSize       大小
	 * @param req            其他请求参数
	 * @return 环节权限列表
	 */
	@ApiOperation(value = "分页列表查询", notes = "flow_permission-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(FlowPermission flowPermission,
								   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
								   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FlowPermission> queryWrapper = QueryGenerator.initQueryWrapper(flowPermission, req.getParameterMap());
		Page<FlowPermission> page = new Page<FlowPermission>(pageNo, pageSize);
		IPage<FlowPermission> pageList = flowPermissionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 * @param flowPermission
	 * @return
	 */
	@ApiOperation(value = "添加环节权限", notes = "flow_permission-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FlowPermission flowPermission) {
		flowPermissionService.save(flowPermission);
		return Result.OK("添加成功！");
	}

	/**
	 * 修改
	 * @param flowPermission 实体类
	 * @return
	 */
	@ApiOperation(value = "修改环节权限", notes = "flow_permission-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FlowPermission flowPermission) {
		flowPermissionService.updateById(flowPermission);
		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "通过id删除环节权限", notes = "flow_permission-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
		flowPermissionService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "通过ids批量删除环节权限", notes = "flow_permission-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.flowPermissionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation(value = "通过id查询环节权限", notes = "flow_permission-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
		FlowPermission flowPermission = flowPermissionService.getById(id);
		if (flowPermission == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(flowPermission);
	}

	/**
	 * 根据企业号或机构号获取流程环节的权限
	 *
	 * @param taskDefId 环节ID
	 * @param cusNo     企业号
	 * @return
	 */
	@ApiOperation("根据企业号或机构号获取流程环节的权限")
	@GetMapping("/getTaskPermissionConfig")
	public Result<TaskPermissionConfigDto> getTaskPermissionConfig(@RequestParam String procdefId, @RequestParam String taskDefId, Integer cusNo, String orgId) {
		TaskPermissionConfigDto taskPermissionConfig = flowPermissionService.getTaskPermissionConfig(procdefId, taskDefId, cusNo, orgId);
		return Result.OK(taskPermissionConfig);
	}

}
